<?php namespace Admin\Controller;

//友情链接控制器
class LinkController extends CommonController{
	//存放模型对象
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\Link;
	}

	//友链列表
	public function index(){
		//分页
		$Page=Page::row(5)->make($this->model->count());
		View::with('Page',$Page);
		//获得数据
		$data=$this->model->orderBy('sort','ASC')->limit(Page::limit())->get();
		if (!$data) View::error('请先添加友链',U('add'));
		View::with('data',$data)->make();
	}

	//添加友链
	public function add(){
		if (IS_POST) {
			if ($this->model->store()) {
				View::success('添加成功',U('index'));
			}
			View::error($this->model->getError());
		}
		View::make();
	}

	//删除友情链接
	public function del(){
		$lid=Q('get.lid');
		$this->model->where("lid=$lid")->delete();
		View::success('删除成功',U('index'));
	}

	//编辑友情链接
	public function edit(){
		if (IS_POST) {
			if($this->model->edit()) View::success('修改成功',U('index'));
			View::error($this->model->getError());
		}
		//获得旧数据
		$lid=Q('get.lid');
		$data=$this->model->where("lid=$lid")->find();
		View::with('data',$data)->make();
	}



}